// 在页面或组件中导入
import validate from 'wechat-validate'
Page({
  // 通过 behaviors 注入 validate 方法
  behaviors: [validate],
  data: {
    currentDate: new Date().getTime() + 1000 * 60 * 60 * 24,
    maxDate: new Date().getTime() + 1000 * 60 * 60 * 24 * 5,
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [],
    houseInfo: '',
    repairItem: [],
    repairItemName: '',
    // 需要提交的数据
    houseId: '',
    repairItemId: "",
    mobile: "",
    appointment: "",
    description: "",
    attachment: [],
  },
  rules: {
    houseId: [
      { required: true, message: '请选择报修房屋!' },
    ],
    repairItemId: [
      { required: true, message: '请选择维修项目!' },
    ],
    mobile: [
      { required: true, message: '请填写手机号码!' },
      { pattern: /^1[3-8]\d{9}$/, message: '请检查手机号码是否正确!' },
    ],
    appointment: [
      { required: true, message: '请选择预约日期!' },
    ],
    description: [
      { required: true, message: '请填写问题描述!' },
    ],
    attachment: [
      { required: true, message: '请上传问题附件!' },
    ],
  },
  onShow() {
    this.getHouseList()
  },
  onLoad({ id }) {
    // console.log(id);
    this.getRepairInfo(id)
  },
  // 获取报修详情
  async getRepairInfo(id) {
    const repairInfo = await wx.http.get('/repair/' + id)
    console.log(repairInfo);
    this.setData({ ...repairInfo })
  },
  // 获取房屋/维修列表
  async getHouseList() {
    const res = await wx.http.get('/room')
    // console.log(houseList);
    const houseList = res.map(({ id, point, building, room }) => ({ id, name: point + building + room }))
    const repairItem = await wx.http.get('/repairItem')
    // console.log(repairItem);
    this.setData({ houseList, repairItem })
  },
  // 选择房屋
  houseSelect(ev) {
    // console.log(ev.detail);
    const { id, name } = ev.detail
    this.setData({ houseId: id, houseInfo: name })
  },
  // 选择维修项目
  repairSelect(ev) {
    // console.log(ev.detail);
    const { id, name } = ev.detail
    this.setData({ repairItemId: id, repairItemName: name })
  },
  // 点击时间确定按钮
  onConfirm(ev) {
    // console.log(ev.detail);
    const appointment = wx.utils.dataFormat(ev.detail)
    this.setData({ appointment, dateLayerVisible: false })
  },
  // 上传图片
  afterRead(ev) {
    // console.log(ev.detail.file.tempFilePath);
    wx.uploadFile({
      filePath: ev.detail.file.tempFilePath,
      name: 'file',
      url: wx.http.baseURL + '/upload',
      header: {
        Authorization: 'Bearer ' + wx.getStorageSync('token')
      },
      formData: { type: 'avatar' },
      success: (res) => {
        const { code, data } = JSON.parse(res.data)
        // console.log(data);
        if (code !== 10000) return wx.utils.toast('上传头像失败！')
        this.data.attachment.push({ id: data.id, url: data.url })
        this.setData({ attachment: this.data.attachment })
      }
    })
  },
  // 删除图片
  deleteImg(ev) {
    console.log(ev.detail.index);
    this.data.attachment.splice(ev.detail.index, 1)
    this.setData({ attachment: this.data.attachment })
  },
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  openRepairLayer() {
    this.setData({ repairLayerVisible: true })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },

  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  // 提交报修
  async goList() {
    if (!this.validate()) return
    const { id, houseId, repairItemId, mobile, appointment, description, attachment } = this.data
    await wx.http.post('/repair', { id, houseId, repairItemId, mobile, appointment, description, attachment })
    wx.utils.toast('成功')
    wx.reLaunch({
      url: '/repair_pkg/pages/list/index',
    })
  },
})
